<?php
/**
 * @file
 * @package mileage
 * @version $Id$
**/

if (!defined('XOOPS_ROOT_PATH')) {
	exit;
}

require_once MILEAGE_TRUST_PATH . '/class/AbstractListAction.class.php';

/**
 * Mileage_RecListAction
**/
class Mileage_RecListAction extends Mileage_AbstractListAction
{
	private $_loginMile;
	/**
	 * &_getHandler
	 * 
	 * @param   void
	 * 
	 * @return  Mileage_MileHandler
	**/
	protected function &_getHandler()
	{
		$handler =& $this->mAsset->getObject('handler', 'rec');
		return $handler;
	}

	public function hasPermission()
	{
		$root =& XCube_Root::getSingleton();
		if (is_object($root->mContext->mXoopsUser)) {
			return TRUE;
		} else {
			return FALSE;
		}
	}

	/**
	 * getDefaultView
	 * 
	 * @param   void
	 * 
	 * @return  Enum
	**/
	public function getDefaultView()
	{
		$this->mFilter =& $this->_getFilterForm();
		$this->mFilter->fetch();
		$handler =& $this->_getHandler();
		$this->mObjects =& $handler->getObjects($this->mFilter->getCriteria());

		$tempMile = array();
		for ($i = 0; $i<count($this->mObjects); $i++) {
			$mile_id = $this->mObjects[$i]->get('mile_id');
			if (isset($tempMile[$mile_id])) {
				$this->mObjects[$i]->mileName = $tempMile[$mile_id];
			} else {
				$mile = $this->mObjects[$i]->getMileObject($this->mAsset->mDirname);
				if (is_object($mile)) {
					$tempMile[$mile_id] = $mile->get('name');
					$this->mObjects[$i]->mileName = $tempMile[$mile_id];
				}
			}
		}

		$root =& XCube_Root::getSingleton();
		$this->_uid = $root->mContext->mXoopsUser->get('uid');

	/*	$this->_recActionForm = NULL;
		if (count($minusMile) > 0) {
			$this->_recActionForm =& $this->mAsset->getObject('form', 'rec', FALSE, 'edit');
		}*/

		// LOGIN MILE
	//	$loginHandler =& $this->mAsset->getObject('handler', 'mile');
	//	$this->_loginMile = $loginHandler->getObjects(new Criteria('url', 'login'));

		return MILEAGE_FRAME_VIEW_INDEX;
	}

	/**
	 * &_getFilterForm
	 * 
	 * @param   void
	 * 
	 * @return  Mileage_RecFilterForm
	**/
	protected function &_getFilterForm()
	{
		// $filter =& new Mileage_RecFilterForm();
		$filter =& $this->mAsset->getObject('filter', 'rec', FALSE);// 3rd arg is FALSE(not admin) 
		$filter->prepare($this->_getPageNavi(), $this->_getHandler());
		return $filter;
	}

	/**
	 * _getBaseUrl
	 * 
	 * @param   void
	 * 
	 * @return  string
	**/
	protected function _getBaseUrl()
	{
		$url_approach = $this->mModule->getModuleConfig('url_approach');
		return Mileage_Utils::makeBaseUrl($this->mAsset->mDirname, 'RecList', $url_approach);
	}

	/**
	 * executeViewIndex
	 * 
	 * @param   XCube_RenderTarget  &$render
	 * 
	 * @return  void
	**/
	public function executeViewIndex(&$render)
	{
		$dirname = $this->mAsset->mDirname;
		XCube_DelegateUtils::call('Module.' . $dirname . '.Event.RecList.executeViewIndex', new XCube_Ref($this->mObjects), new XCube_Ref($render));

		$render->setTemplateName($dirname . '_rec_list.html');
		$render->setAttribute('dirname', $dirname);
		$render->setAttribute('objects', $this->mObjects);
		$render->setAttribute('pageNavi', $this->mFilter->mNavi);
	//	$render->setAttribute('actionForm', $this->_recActionForm);
		$render->setAttribute('uid', $this->_uid);
	/*	if (count($this->_loginMile) > 0) {
			$render->setAttribute('login', TRUE);
		}*/
		$render->setAttribute('getQuery', count($_GET)>0);//TODO
		$render->setAttribute('isMobile', Mileage_Utils::checkMobile());
		$render->setAttribute('xoops_breadcrumbs', Mileage_Utils::breadCrumbs($dirname, _MD_MILEAGE_LANG_REC_LIST));

		// CSS
		$css = XOOPS_URL . '/modules/' . $dirname . '/style.css';
		$moduleHeader = $render->getAttribute('xoops_module_header');
		$moduleHeader .= '<link rel="stylesheet" type="text/css" media="screen" href="'. $css .'" />';
		$render->setAttribute('xoops_module_header', $moduleHeader);
	}

}
